package me.project.jobster.common.util;


import java.rmi.server.UID;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *  主键生成
 * String gid = GUIDGenerator.getInstance().generateUID()
 * @author Mcdull
 */
public class GUIDGenerator {
	/**
	 * 精确到毫秒
	 */
	private static SimpleDateFormat idFormater = new SimpleDateFormat("yyyyMMddHHmmssSSS");

	private static GUIDGenerator myInstance = new GUIDGenerator();

	public static GUIDGenerator getInstance() {
		return myInstance;
	}

	/**
	 * 方法说明：
	 *	生成20位的Key
	 *  @return
	 */
	public synchronized String generateUID() {
		String str = (new UID()).toString();
		String uuid = ""; 
		try {
			uuid = idFormater.format(new Date()) + str.substring(str.length()-3,str.length());
		} catch (Exception e) {
			uuid = idFormater.format(new Date()) + str.replaceAll(":", "_");
		}
		if (uuid.length() > 20) {
			uuid = uuid.substring(0, 20);
		}
		return uuid;
	}
}
